package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.common.R;
import com.itheima.domain.Category;
import com.itheima.domain.Setmeal;
import com.itheima.dto.SetmealDto;
import com.itheima.service.CategoryService;
import com.itheima.service.SetmealDishService;
import com.itheima.service.SetmealDtoService;
import com.itheima.service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("setmeal")
@SuppressWarnings("all")
public class SetmealController {

    @Autowired
    private SetmealDtoService setmealDtoService;

    @Autowired
    private SetmealDishService setmealDishService;

    @Autowired
    private SetmealService setmealService;

    @Autowired
    private CategoryService categoryService;

    @PostMapping
    public R<String> save(@RequestBody SetmealDto setmealDto) {
        setmealService.saveWithDish(setmealDto);
        return R.success("添加菜品成功！");
    }

    @GetMapping("/page")
    public R<Page> page(int page, int pageSize, String name) {
        Page<Setmeal> pageInfo = new Page<>(page, pageSize);

        LambdaQueryWrapper<Setmeal> lqw = new LambdaQueryWrapper<>();
        lqw.like(name != null, Setmeal::getName, name);
        lqw.orderByDesc(Setmeal::getUpdateTime);
        setmealService.page(pageInfo, lqw);

        Page<SetmealDto> pageDto = new Page<>();

        BeanUtils.copyProperties(pageInfo, pageDto, "records");
        List<Setmeal> records = pageInfo.getRecords();
        List<SetmealDto> collect = records.stream().map(itme -> {
            SetmealDto setmealDto = new SetmealDto();
            BeanUtils.copyProperties(itme, setmealDto);
            Long categoryId = itme.getCategoryId();
            Category category = categoryService.getById(categoryId);
            if (category != null) {
                String categoryName = category.getName();
                setmealDto.setCategoryName(categoryName);
            }
            return setmealDto;
        }).collect(Collectors.toList());
        pageDto.setRecords(collect);
        return R.success(pageDto);
    }


    @DeleteMapping
    public R<String> deldte(@RequestParam List<Long> ids) {
        setmealService.removeWithDish(ids);
        return R.success("套餐数据删除成功");
    }

    // 根据条件查询套餐数据
    @GetMapping("/list")
    public R<List<Setmeal>> getList(Setmeal setmeal){

        LambdaQueryWrapper<Setmeal> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Setmeal::getCategoryId,setmeal.getCategoryId());
        lqw.eq(Setmeal::getStatus,setmeal.getStatus());
        lqw.orderByDesc(Setmeal::getUpdateTime);

        List<Setmeal> list = setmealService.list(lqw);
        return R.success(list);
    }
    @GetMapping("/{id}")
    public R<SetmealDto> get(@PathVariable Long id){
        SetmealDto setmealDto = setmealService.getByIdWithFlavor(id);
        return R.success(setmealDto);
    }

    @PutMapping
    public R<String> update(@RequestBody SetmealDto setmealDto) {
       setmealService.updateWithDish(setmealDto);
        return R.success("菜品修改成功");
    }

    @PostMapping("/status/{status}")
    //@pathVariable注解用于接收/后面的参数，@RequestParam注解用于接收?后面的参数
    public R<String> status(@PathVariable Integer status,@RequestParam("ids") List<Long> ids){
        LambdaUpdateWrapper<Setmeal> luw = new LambdaUpdateWrapper();
        //将页面传过来的id跟数据库中的id比较，in（范围内），
        luw.in(Setmeal::getId,ids);
        //将对应上Id数据（满足条件的数据）,根据页面传过来的status integer类型 更改状态信息  1起售  0停售
        luw.set(Setmeal::getStatus,status);
        //根据service接口，调用修改方法，将满足条件修改完状态的数据 进行修改
        setmealService.update(luw);
        return R.success("修改成功");
    }
}